// Windows/DLL.cpp

#include "StdAfx.h"

#include "DLL.h"

namespace Common
{

namespace System
{
namespace DLL
{

bool CLibrary::Free()
{
	if (_module == 0)
	{
		return true;
	}
	// MessageBox(0, TEXT(""), TEXT("Free"), 0);
	// Sleep(5000);
	if (!::FreeLibrary(_module))
	{
		return false;
	}
	_module = 0;
	return true;
}

bool CLibrary::LoadOperations(HMODULE newModule)
{
	if (newModule == NULL)
	{
		return false;
	}
	if (!Free())
	{
		return false;
	}
	_module = newModule;
	return true;
}

bool CLibrary::LoadEx(LPCTSTR fileName, DWORD flags)
{
	return LoadOperations(::LoadLibraryEx(fileName, NULL, flags));
}

bool CLibrary::Load(LPCTSTR fileName)
{
	return LoadOperations(::LoadLibrary(fileName));
}

bool GetModuleFileName(HMODULE hModule, CString &result)
{
	result.Empty();
	DWORD dwSize = ::GetModuleFileName(hModule, result.GetBufferSetLength(_MAX_PATH + 1), MAX_PATH + 1);
	if (dwSize <= MAX_PATH && dwSize != 0)
	{
		dwSize = ::GetModuleFileName(hModule, result.GetBufferSetLength(dwSize + 1), dwSize + 1);
	}
	return 0 != dwSize;
}


}
}

}